#include "StdAfx.h"
#include "StdLog.h"
#include "StdConsole.h"

StdLog * Log = NULL;
const char StdLog::_StdLogFilename[] = "stdlog.txt";
const char StdLog::_ConfigFilename[] = "config\\stdlog.xml";

BEGIN_XML_CONFIG_LOADER(StdLog)
	XML_CONFIG_LOADER_LINK_INTEGER(_Channel, "channel")
	XML_CONFIG_LOADER_LINK_STRING(_CreationMode, "creationmode")
END_XML_CONFIG_LOADER

StdLog::StdLog(void) : XmlConfigLoader(_ConfigFilename)
{
	Log = this;
}

StdLog::~StdLog(void)
{
	Log = NULL;
}

HRESULT StdLog::Create(void)
{
	LogView::LOGVIEWDESC desc;
	desc.channelselection = _Channel;
	errno_t err = strcpy_s(desc.filename, MAX_STRING_LENGTH, _StdLogFilename);
	ASSERT(err == 0);
	if (strcmp(_CreationMode, "CreateNewFile") == 0)
		desc.flags = CreateNewFile;
	else
		desc.flags = OpenExistingFile;
	err = strcpy_s(desc.name, MAX_STRING_LENGTH, "Console");
	ASSERT(err == 0);

	Init(&desc);
	if (desc.channelselection & LogView::Console & (StdConsole::GetInstance() == NULL))
	{
		StdConsole::CreateInstance();
		StdConsole::GetInstance()->AttachClass(ClassStdLog);
	}
	LogSystem::GetInstance()->Attach(shared_ptr<LogView>(this));
	return S_OK;
}
